package com.jrx.anytxn.param.service.investor.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.investor.PrInvestorPreterminationFeeReq;
import com.jrx.anytxn.param.dto.investor.PrInvestorPreterminationFeeRes;
import com.jrx.anytxn.param.entity.PrInvestorPreterminationFee;
import com.jrx.anytxn.param.mapper.ext.ExtPrInvestorPreterminationFeeMapper;
import com.jrx.anytxn.param.service.investor.IPrInvestorPreterminationFeeService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 出资方提前结清手续费表 业务实现
 *
 * @author 黄秋平
 * @date 2020-05-20 10:54
 **/
@Service
@CacheConfig(cacheNames = "INVESTOR_PRETERMINATION")
public class PrInvestorPreterminationFeeServiceImpl  implements IPrInvestorPreterminationFeeService {
    
    private static final Logger logger = LoggerFactory.getLogger(PrInvestorPreterminationFeeServiceImpl.class);

    @Resource
    private ExtPrInvestorPreterminationFeeMapper extPrInvestorPreterminationFeeMapper;

    @Resource
    private ISegmentService segmentService;


    @Override
    public TxnPage<PrInvestorPreterminationFeeRes> findAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<PrInvestorPreterminationFee> page = PageHelper.startPage(pageNum, pageSize, Constant.ORDER_BY_SORT);
        List<PrInvestorPreterminationFee> prPreterminationFeeTableList = extPrInvestorPreterminationFeeMapper.selectByTenId(tenantId);
        if (prPreterminationFeeTableList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PrInvestorPreterminationFeeRes> res = BeanMapping.copyList(prPreterminationFeeTableList, PrInvestorPreterminationFeeRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public PrInvestorPreterminationFeeRes addPreterminationFeeTable(PrInvestorPreterminationFeeReq prPreterminationFeeTableReq) throws TxnException {
        if (prPreterminationFeeTableReq.getMaxAmount() != null && prPreterminationFeeTableReq.getMinAmount() != null && prPreterminationFeeTableReq.getMaxAmount().compareTo(prPreterminationFeeTableReq.getMinAmount()) < 0) {
            throw new TxnBizException(TxnRespCode.BIZ_ERROR.getCode(), "输入有误,最大费用值要比最小费用值大");
        }
        PrInvestorPreterminationFee prPreterminationFeeTable = extPrInvestorPreterminationFeeMapper.selectByPreterminationIdAndOrganizationId(prPreterminationFeeTableReq.getInvestorPreterminationFeeId(), prPreterminationFeeTableReq.getOrganizationId(), prPreterminationFeeTableReq.getTenantId());
        if (prPreterminationFeeTable != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PrInvestorPreterminationFee preterminationFeeTable = BeanMapping.copy(prPreterminationFeeTableReq, PrInvestorPreterminationFee.class);
        try {
            preterminationFeeTable.setId(segmentService.getId(Constant.PR_INVESTOR_PRETERMINATION_FEE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        preterminationFeeTable.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrInvestorPreterminationFeeMapper.insertSelective(preterminationFeeTable);
        return BeanMapping.copy(preterminationFeeTable, PrInvestorPreterminationFeeRes.class);
    }

    @CacheEvict(key = "'investorPretermination_'+#prPreterminationFeeTableReq.tenantId+'_'+#prPreterminationFeeTableReq.investorPreterminationFeeId")
    @Override
    public PrInvestorPreterminationFeeRes modifyPreterminationFeeTable(PrInvestorPreterminationFeeReq prPreterminationFeeTableReq) throws TxnException {
        if (prPreterminationFeeTableReq.getMaxAmount() != null && prPreterminationFeeTableReq.getMinAmount() != null && prPreterminationFeeTableReq.getMaxAmount().compareTo(prPreterminationFeeTableReq.getMinAmount()) < 0) {
            throw new TxnBizException(TxnRespCode.BIZ_ERROR.getCode(), "输入有误,最大费用值要比最小费用值大");
        }
        PrInvestorPreterminationFee prPreterminationFeeTable = extPrInvestorPreterminationFeeMapper.selectByTenIdAndFeeId(prPreterminationFeeTableReq.getTenantId(),prPreterminationFeeTableReq.getInvestorPreterminationFeeId());
        if (prPreterminationFeeTable == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }

        PrInvestorPreterminationFee preterminationFeeTable = BeanMapping.copy(prPreterminationFeeTableReq, PrInvestorPreterminationFee.class);
        // 更新
        extPrInvestorPreterminationFeeMapper.updateByTenIdAndFeeId(preterminationFeeTable);
        return BeanMapping.copy(preterminationFeeTable, PrInvestorPreterminationFeeRes.class);

    }

    @CacheEvict(key = "'investorPretermination_'+#tenantId+'_'+#investorPreterminationFeeId")
    @Override
    public Boolean removeByTenIdAndFeeId(String tenantId, String investorPreterminationFeeId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(investorPreterminationFeeId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrInvestorPreterminationFeeMapper.deleteByTenIdAndFeeId(tenantId, investorPreterminationFeeId);
        return i > 0;
    }

    @Override
    public PrInvestorPreterminationFeeRes findByTenIdAndFeeId(String tenantId, String investorPreterminationFeeId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(investorPreterminationFeeId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrInvestorPreterminationFee preterminationFeeTable = extPrInvestorPreterminationFeeMapper.selectByTenIdAndFeeId(tenantId, investorPreterminationFeeId);
        return BeanMapping.copy(preterminationFeeTable, PrInvestorPreterminationFeeRes.class);
    }

    @Cacheable(key = "'investorPretermination_'+#tenantId+'_'+#investorPreterminationFeeId")
    @Override
    public PrInvestorPreterminationFee findByPreterminationFeeId(String investorPreterminationFeeId, String tenantId) throws TxnBizException {
        PrInvestorPreterminationFee preterminationFeeTable = extPrInvestorPreterminationFeeMapper.selectByTenIdAndFeeId(tenantId,investorPreterminationFeeId);
        if (preterminationFeeTable == null) {
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode(), TxnRespCode.NO_DATA.getMsg(ErrorMsgConstant.PRETERMINATION_FEE));
        }
        return preterminationFeeTable;
    }

}
